<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>留言板</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style-type: none;
        }

        table,
        td,
        tr {
            border: 1px solid #ccc;
            border-collapse: collapse;
        }
        td {
            padding: 10px;
        }
    </style>
</head>

<body>
    <h1>留言板</h1>
    <form id="message-form">
        <label for="message">留言：</label>
        <textarea id="message" name="message" required></textarea>
        <br>
        <input type="submit" value="提交">
    </form>
    <br>
    <table id="message-table">
        <tr>
            <td>留言</td>
            <td>操作</td>
        </tr>
    </table>
    <script>
        const messageForm = document.getElementById('message-form');
        const messageTable = document.getElementById('message-table');
        let messageList = JSON.parse(localStorage.getItem('messages')) || [];

        messageForm.addEventListener('submit', function(event) {
            event.preventDefault();
            const message = document.getElementById('message').value;
            if (message.trim()) {
                messageList.push(message);
                localStorage.setItem('messages', JSON.stringify(messageList));
                renderMessageTable();
                document.getElementById('message').value = ''; // 清空输入框
            }
        });

        function renderMessageTable() {
            messageTable.innerHTML = '';
            const headerRow = document.createElement('tr');
            const thMessage = document.createElement('th');
            const thOperation = document.createElement('th');
            thMessage.innerText = '留言';
            thOperation.innerText = '操作';
            headerRow.appendChild(thMessage);
            headerRow.appendChild(thOperation);
            messageTable.appendChild(headerRow);

            messageList.forEach(function(message, index) {
                const tr = document.createElement('tr');
                const tdMessage = document.createElement('td');
                const tdOperation = document.createElement('td');
                const deleteBtn = document.createElement('button');
                deleteBtn.innerText = '删除';
                deleteBtn.addEventListener('click', function() {
                    messageList.splice(index, 1);
                    localStorage.setItem('messages', JSON.stringify(messageList));
                    renderMessageTable();
                });
                tdMessage.innerText = message;
                tdOperation.appendChild(deleteBtn);
                tr.appendChild(tdMessage);
                tr.appendChild(tdOperation);
                messageTable.appendChild(tr);
            });
        }

        // 初始化表格
        renderMessageTable();
    </script>
</body>

</html>